package com.dotflat.check.controller;

import com.dotflat.check.domain.Card;
import com.dotflat.check.po.SysCardPO;
import com.dotflat.check.service.CardService;
import com.dotflat.rest.controller.RestfulBaseController;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.ExcelOperateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/api/card")
@Slf4j
@Api(tags = "系统卡总表")
public class CardController extends RestfulBaseController<Card, CardService, SysCardPO>
{

    @Autowired
    private  CardService cardService;

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    public void setBaseService(CardService service) {
        super.setBaseService(service);
    }


    @ApiOperation(value = "导入卡",httpMethod = "POST")
    @RequestMapping(value = "/import_card")
    @ResponseBody
    public StatusResponse importFoodFromTemplate(@RequestHeader(TICKET) String ticket,
                                                 @RequestParam MultipartFile file) throws Exception{
        StaffDTO staffDTO = cache.getStaff(ticket);

        InputStream input = file.getInputStream();
        String fileName = file.getOriginalFilename();
        Workbook book = ExcelOperateUtil.getWorkbook(input,fileName);
        Sheet sheet = book.getSheetAt(0);

        List<Card> cardList = new ArrayList<>();

        for (int i = 0 ; i < sheet.getLastRowNum(); i++) {
            Row row1 = sheet.getRow(i+1);
            Cell cell1 = row1.getCell(0);
            Double cellValue1 = Double.valueOf(0);
            if (Objects.nonNull(cell1)){
                cellValue1 = cell1.getNumericCellValue();
            }

            Cell cell2 = row1.getCell(1);
            Double cellValue2 = Double.valueOf(0);
            if (Objects.nonNull(cell2)){
                cellValue2 = cell2.getNumericCellValue();
            }

            Cell cell3 = row1.getCell(2);
            String cellValue3 = "";
            if (Objects.nonNull(cell3)){
                cellValue3 = cell3.getStringCellValue();
            }

            Card card  = new Card();
            card.setCardNo(String.valueOf(cellValue1));
            card.setCardCode(String.valueOf(cellValue2));
            card.setPurposeType(cellValue3);
            card.setAreaId(staffDTO.getAreaId());
            card.setCreatedBy(staffDTO.getId());
            cardList.add(card);
        }
        cardService.insetAll(cardList);
        return StatusResponse.success(cardList);
    }
}
